package com.example.dbmeta.config;

import com.example.dbmeta.meta.common.xml.DestinationDataSource;
import com.example.dbmeta.meta.common.xml.XmlConfigLoader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.util.StringUtils;
import com.zaxxer.hikari.HikariDataSource;

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

@Configuration
@EnableTransactionManagement // 开启事务管理
@EnableJpaRepositories(
        entityManagerFactoryRef = "jpamysqlfirstEntityManagerFactory",
        transactionManagerRef = "jpamysqlfirstTransactionManager",
        basePackages = {"com.example.dbmeta.meta"}
)
public class JpaConfig {
    @Autowired
    private XmlConfigLoader xmlConfigLoader;
    /**
     * 读取数据源配置属性
     */
    @Bean(name = "jpamysqlfirstDataSourceProperties")
    //@ConfigurationProperties(prefix = "spring.datasource.jpamysqlfirst")
    public DataSourceProperties dataSourceProperties() throws Exception{
        DataSourceProperties properties = new DataSourceProperties();
        DestinationDataSource dataSource= xmlConfigLoader.loadDataInstancesFromXml("config.xml").getDestinationDataSource();
        properties.setUrl(dataSource.getUrl());
        properties.setDriverClassName(dataSource.getDriverclassname());
        properties.setUsername(dataSource.getUsername());
        properties.setPassword(dataSource.getPassword());
        return properties;
    }

    /**
     * 创建Hikari数据源连接池
     */
    @Bean(name = "jpamysqlfirstDataSource")
    public HikariDataSource dataSource(@Qualifier("jpamysqlfirstDataSourceProperties") DataSourceProperties dbDataSourceProperties) {
        HikariDataSource dataSource = dbDataSourceProperties.initializeDataSourceBuilder().type(HikariDataSource.class).build();
        if (StringUtils.hasText(dbDataSourceProperties.getName())) {
            dataSource.setPoolName(dbDataSourceProperties.getName());
        }
        return dataSource;
    }

    @Bean(name = "jpamysqlfirstEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder entityManagerFactoryBuilder, @Qualifier("jpamysqlfirstDataSource") DataSource db1DataSource) {
        return entityManagerFactoryBuilder.dataSource(db1DataSource)
                .packages("com.example.dbmeta.meta")
                .persistenceUnit("jpamysqlfirst")
                .properties(createHibernateProperties())
                .build();
    }
    private Map<String, String> createHibernateProperties() {
        Map<String, String> properties = new HashMap<>();
        properties.put("hibernate.hbm2ddl.auto", "none");
        properties.put("hibernate.dialect", "org.hibernate.dialect.MySQL8Dialect");
        return properties;
    }
    @Bean(name = "jpamysqlfirstTransactionManager")
    public PlatformTransactionManager transactionManager(@Qualifier("jpamysqlfirstEntityManagerFactory") EntityManagerFactory db1EntityManagerFactory) {
        return new JpaTransactionManager(db1EntityManagerFactory);
    }
}